<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" style="height:40px;">
        <span>电商后台管理项目</span>
      </div>
      <el-button type="info" @click="logOut">退出</el-button>
    </el-header>
    <!-- 主题区域 -->
    <el-container>
      <!-- 左侧区域 -->
      <el-aside :width="iscollapse?'64px':'200px'">
        <!-- 折叠按钮 -->
        <div class="toggleCollapse" @click="toggleCollapse">|||</div>
        <!-- 菜单 -->
        <el-menu :default-active="activeitem" class="el-menu-vertical-demo" background-color="#373d41" text-color="#fff" active-text-color="#108ee9" :collapse="iscollapse" :collapse-transition="false" router>
          <el-submenu v-for="item in menuslist" :key="item.id" :index="item.id+''">
            <!-- 一级 -->
            <template slot="title">
              <i :class="iconsobject[item.id]" />
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级 -->
            <el-menu-item v-for="subitem in item.children" :key="subitem.id" :index="'/'+subitem.path" @click="saveactiveindex('/'+subitem.path)">
              <template slot="title">
                <i class="el-icon-menu" />
                <span>{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧区域 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// @ is an alias to /src
export default {
  name: 'Home',
  data() {
    return {
      menuslist: [],
      iconsobject: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-users',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      iscollapse: false,
      activeitem: ''
    }
  },
  created() {
    this.getmenu()
    this.activeitem = window.sessionStorage.getItem('activeIndex')
  },
  methods: {
    logOut() {
      window.sessionStorage.clear()
      this.$router.push('/Login')
    },
    async getmenu() {
      const { data: list } = await this.$http.get('menus')
      if (list.meta.status !== 200) {
        return this.$message.error(list.meta.msg)
      } else {
        this.menuslist = list.data
        console.log(list.data)
      }
    },
    toggleCollapse() {
      this.iscollapse = !this.iscollapse
    },
    saveactiveindex(index) {
      window.sessionStorage.setItem('activeIndex', index)
      this.activeitem = index
    }
  }
}
</script>
<style lang="scss">
.el-submenu__title {
  text-align: left;
}
.el-submenu .el-menu-item {
  height: 50px;
  line-height: 50px;
  padding: 0 45px;
  min-width: 200px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}
</style>
<style lang="scss" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  div {
    display: flex;
    justify-content: space-between;
    padding: 0;
    align-items: center;
  }
}
.el-aside {
  background: #373d41;
  .el-menu {
    border-right: none;
    .iconfont {
      margin-right: 5px;
    }
  }
}
.el-main {
  background: #fff;
}
.toggleCollapse {
  width: 100%;
  text-align: center;
  color: #fff;
  letter-spacing: 10px;
  background: cornflowerblue;
  cursor: pointer;
  height: 30px;
  line-height: 30px;
}
</style>>
